﻿/*  This file is part of opinhost.
 *
 *  opinhost is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  opinhost is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with opinhost.  If not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Text;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;
using System.Net.Sockets;

namespace web_client
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            txtIp.Text = Dns.GetHostName();
            txtPort.Text = "11000";
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            lstMain.Visible = true;
            txtMain.Visible = true;
            lstMain.Items.Add(new ListItem("Starting Application..."));
            //Data buffer for incoming data.
            byte[] bytes = new byte[1024];

            //Connect to a remote device.
            try
            {
                //Establish the remote endpoint for the socket.
                //This example uses port 11000 on the local computer.
                IPHostEntry ipHostInfo = Dns.Resolve(txtIp.Text);
                IPAddress ipAddress = ipHostInfo.AddressList[0];
                String ipString = ipAddress.ToString();
                IPEndPoint remoteEP = new IPEndPoint(ipAddress, int.Parse(txtPort.Text));
                String portString = remoteEP.Port.ToString();
                //write
                lstMain.Items.Add("IP Address: " + ipString);
                lstMain.Items.Add("Port: " + portString);

                //Create a TCP/IP socket.
                Socket tcpsender = new Socket(AddressFamily.InterNetwork,
                    SocketType.Stream, ProtocolType.Tcp);

                //Connect the socket to the remote endpoint. Catch any errors.
                try
                {
                    tcpsender.Connect(remoteEP);

                    lstMain.Items.Add("Socket connected to " +
                        tcpsender.RemoteEndPoint.ToString());
                    //Encode the data string into a byte array.
                    lstMain.Items.Add("Enter the text to be sent: teststring");
                    String tts = drpCommand.SelectedItem.Value;
                    byte[] msg = Encoding.ASCII.GetBytes(tts + "<EOF>");

                    //Send the data through the socket.
                    int bytesSent = tcpsender.Send(msg);

                    //Receive the response from the remote device.
                    int bytesRec = tcpsender.Receive(bytes);
                    lstMain.Items.Add("Response Length: " + Encoding.ASCII.GetString(bytes, 0, bytesRec).Length);
                    txtMain.Text = Encoding.ASCII.GetString(bytes, 0, bytesRec);

                    //Release the socket.
                    tcpsender.Shutdown(SocketShutdown.Both);
                    tcpsender.Close();
                    lstMain.Items.Add("Press any key to continue...");

                }
                catch
                {
                    lstMain.Items.Add("Error");
                }
            }
            catch
            {

            }
        }
    }
}
